package com.leo.model;

/**
 * @ClassName SimpleLinkedList
 * @Description 单链表实现(线程不安全)
 * @Author wangss
 * @date 2022.01.29 15:30
 * @Version 1.0
 */
public class SimpleLinkedList<E> {

    private class Node {
        public E e;

        public Node next;

        public Node() {
            this(null);
        }

        public Node(E e) {
            this(null, null);
        }

        public Node(E e, Node next) {
            this.e = e;
            this.next = next;
        }
    }

    // 虚拟链表头，用来防止为空
    private Node dummyHead;

    private int size;

    public SimpleLinkedList() {
        dummyHead = new Node();
        size = 0;
    }

    /**
     * 获取元素个数
     *
     * @return
     */
    public int getSize() {
        return size;
    }

    /**
     * 根据索引获取元素
     *
     * @param index
     * @return
     */
    public E get(int index) {
        return getNode(index).e;
    }

    /**
     * 根据索引获取节点
     *
     * @param index
     * @return
     */
    public Node getNode(int index) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("Get failed. Illegal index.");
        }

        Node cur = dummyHead.next;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        return cur;
    }

    /**
     * 获取首元素
     *
     * @return
     */
    public E getFirst() {
        return get(0);
    }

    /**
     * 获取最后一个元素
     *
     * @return
     */
    public E getLast() {
        return get(size - 1);
    }

    /**
     * 返回链表是否为空
     *
     * @return
     */
    public boolean isEmpty() {
        return size == 0;
    }

    /**
     * 往链表末尾插入元素
     *
     * @param e
     */
    public SimpleLinkedList add(E e) {
        return add(size, e);
    }

    /**
     * 往链表头部插入元素
     *
     * @param e
     */
    public SimpleLinkedList addFirst(E e) {
        return add(0, e);
    }

    /**
     * 在链表指定索引位置插入元素
     *
     * @param index
     * @param e
     */
    public SimpleLinkedList add(int index, E e) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("Get failed. Illegal index.");
        }

        Node pre = dummyHead;
        for (int i = 0; i < index; i++) {
            pre = pre.next;
        }
        pre.next = new Node(e, pre.next);
        size++;
        return this;
    }

    /**
     * 移除指定索引的元素
     *
     * @param index
     */
    public SimpleLinkedList remove(int index) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("Get failed. Illegal index.");
        }

        Node pre = dummyHead;
        for (int i = 0; i < index; i++) {
            pre = pre.next;
        }
        Node removeNode = pre.next;
        pre.next = removeNode.next;
        removeNode.next = null;
        size--;
        return this;
    }

    /**
     * 移除最后一个元素
     */
    public SimpleLinkedList removeLast() {
        return remove(size - 1);
    }

    /**
     * 移除第一个元素
     */
    public SimpleLinkedList removeFirst() {
        return remove(0);
    }

    /**
     * 反转
     *
     * @return
     */
    public SimpleLinkedList resver() {
        // 上一个节点
        Node pre = null;
        // 下一个节点
        Node next = null;

        Node head = dummyHead.next;
        while (null != head) {
            next = head.next;
            head.next = pre;
            pre = head;
            head = next;
        }
        dummyHead.next = pre;
        return this;
    }

    /**
     * 展示链表信息
     *
     * @param head
     */
    public void showNode(Node head) {
        StringBuilder sb = new StringBuilder();
        while (null != head) {
            sb.append(head.e);
            if (null != head.next) {
                sb.append(" -> ");
            }
            head = head.next;
        }
        System.out.println(sb.toString());
    }
}
